<template>
  <div>
    <el-form ref="vipform" :model="vipData" :rules="vipRules" label-width="120px">
      <el-form-item label="会员卡名称" prop="name">
        <el-input v-model="vipData.name" placeholder="请输入会员卡名称" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="会员卡金额" prop="money"> 
        <el-input v-model="vipData.money" placeholder="请输入会员卡金额" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="有效时间">
        <el-select v-model="vipData.validity_time" placeholder="请选择有效时间" size="small" style="max-width:70">
          <el-option label="七天" value="1"></el-option>
          <el-option label="一个月" value="2"></el-option>
          <el-option label="三个月" value="3"></el-option>
          <el-option label="一年" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="购买次数">
        <el-input v-model="vipData.pay_count" placeholder="请输入购买次数" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="vipData.status" active-color="#13ce66"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import store from '@/store'
import { postPayVip, putPayVip } from '@/api/vip'

export default {
  name: 'vipPanel',
  props: {
    isAdd: {// 是否是新增
      type: Boolean,
      required: true,
      default: true
    },
    vipData: {
      type: Object,
      default: () => {
        return {
          key: store.state.app.activeApp.saa_key,
          name: '',
          money: '',
          validity_time: '1',
          pay_count: '',
          status: true
        }
      }
    }
  },
  data() {
    return {
      vipRules: {
        name: [
          { required: true, message: '请输入会员卡名称', trigger: 'blur' },
        ],
        money: [
          { required: true, message: '请输入会员卡金额', trigger: 'blur' },
        ]
      }
    }
  },
  
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.vipform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            this.vipData.status = this.vipData.status ? '1' : '0';
            postPayVip(this.vipData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            const params = {
              key: this.$store.state.app.activeApp.saa_key,
              id: this.vipData.id,
              name: this.vipData.name,
              money: this.vipData.money,
              validity_time: this.vipData.validity_time,
              pay_count: this.vipData.pay_count,
              status: this.vipData.status ? '1' : '0',
            }
            putPayVip(params).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })
      
    }
  },
}
</script>